<?php
namespace Admin\Controller;
use Common\Model\CommonModel;
use Think\Controller;

class StyleController extends BaseController{

    public function index(){
        $styleModel=M('style');

        $condition['name'] = array('like',"%".$_GET['name']."%");
        unset($_SESSION['like']['designer']);
        $_SESSION['like']['designer']=$_GET['name'];

        $count = $styleModel->where($condition)->count();
        $page= new \Think\Page($count,5);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$styleModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
        $list=CommonModel::status($list);
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }

    public function create(){
        if(!empty($_POST)){
            $data['name']=$_POST['name'];
            $data['create']=time();
            $styleModel=D('style');
            $res=$styleModel->create($data);
            if($res){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(2);
            }
        }
        $this->display();

    }

    public function update(){
        $id=$_GET['id'];
        $styleModel=D('style');
        $list=$styleModel->finded('id='.$id);
        $this->assign('list',$list);
        $this->display();

    }

    public function doUpdate(){
        $styleModel=D('style');
        $data['name']=$_POST['name'];
        $id=$_POST['id'];
        $where='id='.$id;
        // echo $id;exit;
        $list=$styleModel->finded($where);
        /* echo(json_encode($list));
         exit;*/
        $data['update']=time();
        //过滤重复内容
        $data=CommonModel::filter($data,$list);
        $res=$styleModel->update($where,$data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }
    }


    public function del(){
        $styleModel=D('style');
        $id=$_GET['id'];
        $res= $styleModel->deleted('id='.$id);
        if($res){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败');
        }
    }






}